<?php

define ('NUMBER_OF_ROUTES', 5);
require_once("storage/predefRoutes.php");
error_reporting(E_WARNING);
    
    class test2 extends abstract_controller {
        
        function prepare_model($request_params, Smarty $smarty_obj) {

            if (isset($_GET['date'])){
            	$date = htmlspecialchars($_GET['date']);
            } else {
            	$date = date('Y-m-d', strtotime('+1 day'));
            }
            $selectedDay = date("w", strtotime($date));
           	$day = date("w");
            
            $routes = array();
            $places = array();
            $forecasts = array();
            
            $weekInNumbers = array();
            $weekInNumbers[] = date("Y-m-d", strtotime("now"));
            for($j=1;$j<6;$j++) {
            	$weekInNumbers[] = date("Y-m-d", strtotime("+" . $j . " days"));
            }
            
            $start_day_number = null;
            switch($selectedDay) {
            	case 0: $selectedDay = "søndag"; $start_day_number = 6; break;            	
            	case 1: $selectedDay = "mandag"; $start_day_number = 0; break;
            	case 2: $selectedDay = "tirsdag"; $start_day_number = 1; break;
            	case 3: $selectedDay = "onsdag"; $start_day_number = 2; break;
            	case 4: $selectedDay = "torsdag"; $start_day_number = 3; break;
            	case 5: $selectedDay = "fredag"; $start_day_number = 4; break;
            	case 6: $selectedDay = "lørdag"; $start_day_number = 5; break;
            
            }
$start_day_number= date('w');
            $week = array();
            $weektext = array();
            
            $tempday = $start_day_number;
            $steps_moved = null;
            
            for($j=0; $j<6; $j++) {
            	if($tempday==0) $steps_moved = $j;
            	$week[] = $tempday%7;
            	$tempday++;
            }
            
            for($j=0;$j<6;$j++) {
	            switch($week[$j]) {
	            	case 1: $weektext[] = "Mandag"; break;
	            	case 2: $weektext[] = "Tirsdag"; break;
	            	case 3: $weektext[] = "Onsdag"; break;
	            	case 4: $weektext[] = "Torsdag"; break;
	            	case 5: $weektext[] = "Fredag"; break;
	            	case 6: $weektext[] = "Lørdag"; break;
	            	case 0: $weektext[] = "Søndag"; break;
	            }
           	}
           	
           	//echo '<pre>';print_r($_GET);echo '</pre>';
            
			$counter = 0;
			$placeStr = '';
			$route0;
            for($i=1; $i<= NUMBER_OF_ROUTES; $i++){
            	if (isset($_GET['id'.$i])){
            		if ($counter < 2){
					
						
						$route = predefRoutes::getRoute($i);
						
					//       $currentPlaces = array();
					//        $currentPlaces[] = $route->getStartPlace();
					//	$currentPlaces[] = predefRoutes::getRoute($i)->getMiddlePlace();
					//       $currentPlaces[] = predefRoutes::getRoute($i)->getEndPlace();
                
   			            $route->getStartPlace()->setForecast();
		                $route->getMiddlePlace()->setForecast();
        		        $route->getEndPlace()->setForecast();
                
		                $route->getStartPlace()->setCurrentForecast($date);
        		        $route->getMiddlePlace()->setCurrentForecast($date);
                		$route->getEndPlace()->setCurrentForecast($date);
                
		                $places[] = array(0 => $route->getStartPlace(), 1 => $route->getMiddlePlace(), 2=> $route->getEndPlace());
		                
						$counter++;
			           	$placeStr .= '&id'.$i;
						
						$route->calculateScores();
				
						if(empty($route0)){
							$route0 = $route;
						}else{
							$route0->calculateBestRoute($route->getScore());
							$route->calculateBestRoute($route0->getScore());
						}
						
						$routes[] = $route;
              
				
				
            		}
            	}
            }
            
			
			
//	foreach($routes as $route){
	//	echo $route->getName();
		
//		           echo "<pre>";
 //            print_r($route);
 //               echo "</pre>";
  //              exit();
		
//		}
		
	
         /*   foreach($_POST as $line_num => $line) {
                $routes[] = predefRoutes::getRoute($line);
                $currentPlaces = array();
                $currentPlaces[] = predefRoutes::getRoute($line)->getStartPlace();
                $currentPlaces[] = predefRoutes::getRoute($line)->getMiddlePlace();
                $currentPlaces[] = predefRoutes::getRoute($line)->getEndPlace();
                
                $currentPlaces[0]->setForecast();
                $currentPlaces[1]->setForecast();
                $currentPlaces[2]->setForecast();
                
                $currentPlaces[0]->setCurrentForecast($date);
                $currentPlaces[1]->setCurrentForecast($date);
                $currentPlaces[2]->setCurrentForecast($date);
                
                $places[] = array(0 => $currentPlaces[0], 1 => $currentPlaces[1], 2=> $currentPlaces[2]);
                
//                echo "<pre>";
//                print_r($currentPlaces[2]);
//                echo "</pre>";
//                exit();
            }
       */     
;
			
//			$start_day_number = $tempday-$steps_moved;
	
	
            $id = array(1,2,3);
            $smarty_obj->assign('id', $id);
			$smarty_obj->assign('placeStr', $placeStr);
            $smarty_obj->assign('routes', $routes);
            $smarty_obj->assign('places', $places);
            $smarty_obj->assign('date', $date);
    //        $smarty_obj->assign('day', $day);
            $smarty_obj->assign('selectedDay', $selectedDay);
            $smarty_obj->assign('week', $weektext);
            $smarty_obj->assign('weekInNumbers', $weekInNumbers);
//            $smarty_obj->assign('startdaynumber', $start_day_number);
            $smarty_obj->assign('page_title', 'TurYr: Værmelding');
        }


    }

?>
